﻿using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.Models;
using IPR_StudyMeetingMrgSys.ViewModel;
using IPR_StudyMeetingMrgSys.Common;

using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Web;
using System.Web.Http;
using System.Web.Http.Cors;

namespace IPR_StudyMeetingMrgSys.Controllers
{
    public class DepController : ApiController
    {
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        BLL.Dep BllDep = new BLL.Dep();
        [HttpPost]
        [Route("api/Dep/Add")]
        public IHttpActionResult AddDep(VM_Dep model)
        {
            log.Info($"DepID:{model.DepID};DepName:{model.DepName};DepPID:{model.DepPID}");

            ResultMsg resultMsg = new ResultMsg();
            int depid= new BLL.SysManage().GetMaxId("Dep", "DepID");
            //验证部门ID DepID是否存在
            if (BllDep.Exists(model.DepName))
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = "已经存在该部门名称";
                resultMsg.Data = "";
            }
            else
            {
                Model.Dep mdep = BllDep.GetModel(model.DepPID);

                Model.Dep MolDep = new Model.Dep();
                MolDep.DepID = depid;
                MolDep.DepName = model.DepName;
                MolDep.DepPID = model.DepPID;
                MolDep.IsDelete = 0;
                MolDep.DepLevel = mdep.DepLevel + 1; 
                if (BllDep.Add(MolDep)>0)
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = null;
                }
            }

            return Json(resultMsg);
        }
        
        [HttpPost]
        [Route("api/Dep/Sel")]
        public IHttpActionResult GetDepList()
        {
            log.Info($"获取部门列表");
            ResultMsg resultMsg = new ResultMsg();
            DataTable dt= BllDep.GetAllList().Tables[0];
            if (dt.Rows.Count > 0)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = dt;
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "没有获取到部门信息";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }

        [HttpPost]
        [Route("api/Dep/GetDepAndParentDepList")]
        public IHttpActionResult GetDepAndParentDepList()
        {
            log.Info($"获取部门列表");
            ResultMsg resultMsg = new ResultMsg();
            DataTable dt = BllDep.GetDepAndParentDepList().Tables[0];
            if (dt.Rows.Count > 0)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = dt;
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "没有获取到部门信息";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }
        /// <summary>
        /// 获取部门的树结构
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Dep/SelTree")]
        public IHttpActionResult GetDepTreeList()
        {
            log.Info($"获取部门列表");
            ResultMsg resultMsg = new ResultMsg();
            DataTable dt = BllDep.GetAllList().Tables[0];
            if (dt.Rows.Count > 0)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                List<VM_Tree> VM_TreeList = new List<VM_Tree>();
                DataRow[] dataRow0 = dt.Select("DepPID=1");
                Tree.GetTree(dt,dataRow0,ref VM_TreeList, "DepID", "DepPID", "DepName");
                resultMsg.Data = VM_TreeList;
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "没有获取到部门信息";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 获取部门树及部门内人员的所有树结构
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Dep/SelTreeDeptUser")]
        public IHttpActionResult GetDepUserTreeList()
        {
            log.Info($"获取部门列表");
            ResultMsg resultMsg = new ResultMsg();
            DataTable dt = BllDep.GetDepUserTreeList().Tables[0];
            if (dt.Rows.Count > 0)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                List<VM_Tree> VM_TreeList = new List<VM_Tree>();
                DataRow[] dataRow0 = dt.Select("DepPID=1");
                Tree.GetTree(dt, dataRow0, ref VM_TreeList, "DepID", "DepPID", "DepName");
                resultMsg.Data = VM_TreeList;
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "没有获取到部门信息";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }

        #region 部门
        #region 读取当前部门的 第一级的子级部门列表
        [HttpPost]
        [Route("api/Dep/SelNextTree")]
        public IHttpActionResult GetNextDepTreeList(VM_Dep model)
        {
            //int CurrentDeptId = LoginUser.UserInfo.DeptID ?? 0;
            //获取下级部门列表  只读去 仅次于当前级别的 下级部门  
            ResultMsg resultMsg = new ResultMsg();
            DataTable dt = BllDep.GetList("DepPID='" + model.DepID + "'").Tables[0];
            if (dt.Rows.Count > 0)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                var VM_TreeList = Tree.GetVM_TreeList(dt);
                resultMsg.Data = VM_TreeList;
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "没有获取到部门信息";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }
        #endregion

        #region 读取当前部门级别，当前部门id
        [HttpPost]  
        [Route("api/Dep/DeptLevel")]
        public IHttpActionResult GetDeptLevel()
        {
            //获取当前 部门级别   如  段:0
            ResultMsg resultMsg = new ResultMsg();
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
            int currentDeptLevelId = LoginUser.DepLevel ?? 0;
            int cuurentDeptId = LoginUser.DepID??0;
            int currentPreDepId = LoginUser.DepPID ?? 0;
            resultMsg.Data =new { currentDeptLevelId=currentDeptLevelId, cuurentDeptId=cuurentDeptId, currentPreDepId=currentPreDepId, UserID = LoginUser.UserInfo.UserID};
            return Json(resultMsg);
        }
        #endregion

        #endregion

        

        [HttpPost]
        [Route("api/Dep/GetDepInfo")]
        public IHttpActionResult GetDepInfo()
        {
            log.Info($"获取部门列表并形成树结构所需的json串");
            ResultMsg resultMsg = new ResultMsg();
            DataTable dt = BllDep.GetAllList().Tables[0];
            if (dt.Rows.Count > 0)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = dt;
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "没有获取到部门信息";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }
        [HttpPost]
        [Route("api/Dep/Del")]
        public IHttpActionResult DelDep(VM_Dep model)
        {
            log.Info($"删除部门信息");
            ResultMsg resultMsg = new ResultMsg();
            
            if (BllDep.Delete(model.DepID))
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = "";
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "删除部门信息失败";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }
        [HttpPost]
        [Route("api/Dep/Edit")]
        public IHttpActionResult UpdateDep(VM_Dep model)
        {
            log.Info($"更新部门信息");
            ResultMsg resultMsg = new ResultMsg();
            Model.Dep MolDep = new Model.Dep();
            MolDep.DepID = model.DepID;
            MolDep.DepName = model.DepName;
            MolDep.DepPID = model.DepPID;
            if (BllDep.Update(MolDep))
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = "";
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "更新部门信息失败";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }
    }
}
